package com.example.fengyungo_user.activity.me;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.fengyungo_user.R;
import com.example.fengyungo_user.activity.BaseActivity;
import com.example.fengyungo_user.adapter.MyOrderAdapter;
import com.example.fengyungo_user.constant.ResultConstant;
import com.example.fengyungo_user.constant.UserConstant;
import com.example.fengyungo_user.domain.OrderDetailsVO;
import com.example.fengyungo_user.utils.OkHttpUtils;
import com.example.fengyungo_user.utils.SharedPreferencesUtils;
import com.example.fengyungo_user.utils.ThreadPoolUtil;

import java.util.List;

/**
 * 我的中我的订单页面
 *
 * @author XLR
 */
public class MyOrdersActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "MyOrdersActivity";

    /**
     * 返回按钮
     */
    ImageButton back;
    /**
     * 用户订单的RecyclerView
     */
    RecyclerView recyclerView;
    /**
     * 用户id
     */
    String userId;
    /**
     * 圆形进度条
     */
    ProgressBar progressBar;
    /**
     * 刷新
     */
    SwipeRefreshLayout swipeRefreshLayout;
    /**
     * LinearLayoutManager
     */
    LinearLayoutManager layoutManager;
    /**
     * MyOrderAdapter
     */
    MyOrderAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_orders);
        // todo 我的订单列表中没有订单时提示无订单

        progressBar = findViewById(R.id.progress_bar);
        recyclerView = findViewById(R.id.recyclerview_my_orders);

        back = findViewById(R.id.back);
        back.setOnClickListener(this);

        // 通过SharedPreferences获取司机id
        userId = SharedPreferencesUtils.getUserInfo(this).getUserId();

        // 获取用户的订单
        getOrdersByUser();

        // 下拉刷新
        swipeRefreshLayout = findViewById(R.id.refresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.soilyellow);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getOrdersByUser();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back) {
            finish();
        }
    }

    /**
     * 获取司机已接的订单
     */
    private void getOrdersByUser() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    // 发送GET请求到后端，并接收响应数据
                    String url = UserConstant.URL_USER_USER + "/orders/" + userId;
                    String responseData = OkHttpUtils.get(url);

                    JSONObject jsonObject = JSONObject.parseObject(responseData);
                    String data = jsonObject.getString(ResultConstant.DATA);
                    List<OrderDetailsVO> orders = JSON.parseArray(data, OrderDetailsVO.class);
                    Log.d(TAG, "run: orders:" + orders);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.GONE);
                            layoutManager = new LinearLayoutManager(MyOrdersActivity.this);
                            recyclerView.setLayoutManager(layoutManager);
                            adapter = new MyOrderAdapter(orders);
                            recyclerView.setAdapter(adapter);
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MyOrdersActivity.this, getString(R.string.connection_error), Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        };
        ThreadPoolUtil.execute(runnable);
    }
}